/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.executableTasks;

import javax.microedition.lcdui.Image;
import javax.microedition.location.Coordinates;

import com.aroundj2me.location.LocationService;
import com.aroundj2me.model.ExecutableTask;
import com.aroundj2me.model.Messages;
import com.aroundj2me.utility.HttpCommunicator;

public class GoogleMapsRetriever extends ExecutableTask {
	private final String key = "ABQIAAAA008FGL6WDtMr2ELm_3UvGhTwa4JwLYAJxmob2w5Mp9c8CPT1rhSsiywOo4EgCXUaUvmkobVaRrsbrw";
	private final String gmapsBaseURL = "http://maps.google.com/staticmap?";

	private LocationService locationService;

	// available map types
	public static final String MAP_TYPE_NORMAL = "mobile";
	public static final String MAP_TYPE_SATELLITE = "satellite";
	public static final String MAP_TYPE_TERRAIN = "terrain";
	public static final String MAP_TYPE_HYBRID = "hybrid";

	// map properties
	private Coordinates currentPosition;
	private Coordinates pointOfInterest;
	private int zoomLevel = -1;
	private int width = 256;
	private int height = 256;
	private String mapType = MAP_TYPE_NORMAL;

	public GoogleMapsRetriever(LocationService locationService) {
		this.locationService = locationService;
	}

	private String createQueryURL() {
		// http://maps.google.com/staticmap?center=40.714728,-73.998672&zoomLevel=14&size=512x512&maptype=mobile&markers=40.702147,-74.015794,blues%7C40.711614,-74.012318,greeng%7C40.718217,-73.998284,redc&key=MAPS_API_KEY&sensor=false

		StringBuffer queryURL = new StringBuffer();
		queryURL.append(gmapsBaseURL);

		// set the center of the map
		queryURL.append("center=");
		queryURL.append(currentPosition.getLatitude());
		queryURL.append(",");
		queryURL.append(currentPosition.getLongitude());

		// set map zoomLevel
		if (zoomLevel > 0) {
			queryURL.append("&zoom=");
			queryURL.append(zoomLevel);
		}

		// set size
		queryURL.append("&size=");
		queryURL.append(width);
		queryURL.append("x");
		queryURL.append(height);

		// set maptype
		queryURL.append("&maptype=");
		queryURL.append(mapType);

		// add markers
		queryURL.append("&markers=");
		// current position as red marker
		queryURL.append(currentPosition.getLatitude());
		queryURL.append(",");
		queryURL.append(currentPosition.getLongitude());
		queryURL.append(",reds%7C");
		// point of interest as blue marker
		queryURL.append(pointOfInterest.getLatitude());
		queryURL.append(",");
		queryURL.append(pointOfInterest.getLongitude());
		queryURL.append(",bluet");

		// add key
		queryURL.append("&key=");
		queryURL.append(key);

		queryURL.append("&sensor=false");

		return queryURL.toString();
	}

	public void run() {
		super.run();
		
		try {
			this.currentPosition = locationService.getCurrentLocation()
					.getQualifiedCoordinates();

			// create the HTTP-GET request url
			String url = createQueryURL();

			// execute the HTTP request to the Google Map API
			byte[] returnedData;
			returnedData = HttpCommunicator.executeHTTPGet(url);
			Object result = Image.createImage(returnedData, 0,
					returnedData.length);
			notifyTaskFinished(result);
		} catch (Exception ex) {
			notifyError(Messages.GOOGLEMAP_ERROR);
		}
	}

	public void resetDefault() {
		zoomLevel = -1;
		mapType = MAP_TYPE_NORMAL;
	}

	public void setCurrentPosition(Coordinates currentPosition) {
		this.currentPosition = currentPosition;
	}

	public void setPointOfInterest(Coordinates pointOfInterest) {
		this.pointOfInterest = pointOfInterest;
	}

	public void setZoomLevel(int zoomLevel) {
		zoomLevel = (zoomLevel < 0) ? 1 : zoomLevel;
		zoomLevel = (zoomLevel > 19) ? 19 : zoomLevel;

		this.zoomLevel = zoomLevel;
	}

	public void setWidth(int width) {
		// max width
		width = (width > 640) ? 640 : width;

		this.width = width;
	}

	public void setHeight(int height) {
		// max height
		height = (height > 640) ? 640 : height;

		this.height = height;
	}

	public void setMapType(String mapType) {
		this.mapType = mapType;
	}

}
